const helpDao = require('../dao/helpDao');
const async = require('async');
const _ = require('lodash');
const errorTip = require('../util/errorTip');
const request = require("../util/request");
const baseModel = require('./baseModel');

const HelpModel = Object.assign({

	dao: helpDao,

	save : function (req,callback) {
		
		_.each(req.body,function(value,key){
			req.sanitizeBody(key).trim();
		});		

		req.checkBody({
			title: {
				notEmpty: {
					errorMessage: errorTip['不可为空'].message
			    }
			},
			content: {
				notEmpty: {
					errorMessage: errorTip['不可为空'].message
			    }
			},
		});

		req.getValidationResult().then(function(result) {
			
			if(result.isEmpty()){

				let doc = _.clone(req.body);
				helpDao.save(doc,function(err,doc){
					callback(err,doc);
				});
			}
			else{
				let err = result.array({useFirstErrorOnly: true})[0];
				callback(err);
			}

		});

	},

	getHelpList: function(req,callback){

		this.getList(req, (err, ret) => {
			callback(err, ret);
		});	

	},

}, baseModel);

module.exports = HelpModel;